package com.cskaoyan.utils;

import org.springframework.stereotype.Component;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author: lds
 * @clsss: Md5Util
 */
@Component
public class Md5Util {

    static String salted;

    public static String getMd5(String content) {
        content = content + salted;
        //获得消息摘要算法
        MessageDigest messageDigest = null;
        try {
            messageDigest = MessageDigest.getInstance("md5");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        byte[] contentBytes = content.getBytes();
        byte[] resultBytes = messageDigest.digest(contentBytes);//byte 8 2^8 256  ff255
        int length = resultBytes.length;
        System.out.println("length: " + length);
        StringBuffer stringBuffer = new StringBuffer();
        for (byte resultByte : resultBytes) {
            int temp = resultByte & 0xff;  //介于0-255
            String s = Integer.toHexString(temp);
            if (s.length() == 1) {
                stringBuffer.append(0);
            }
            stringBuffer.append(s);
        }
        String result = stringBuffer.toString();
        String substring = result.substring(1, 7);
        return substring;

    }
}
